package cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.part;

import cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.part.vo.AutoCodePartPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.part.vo.AutoCodePartRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.part.vo.AutoCodePartSaveReqVO;
import cn.iocoder.yudao.module.biz.dal.dataobject.contract.autocode.AutoCodePartDO;
import cn.iocoder.yudao.module.biz.service.contract.autocode.part.AutoCodePartService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 编码生成规则组成")
@RestController
@RequestMapping("/auto/code-part")
@Validated
public class AutoCodePartController {

    @Resource
    private AutoCodePartService codePartService;

    @PostMapping("/create")
    @Operation(summary = "创建编码生成规则组成")
    @PreAuthorize("@ss.hasPermission('auto:code-part:create')")
    public CommonResult<Long> createCodePart(@Valid @RequestBody AutoCodePartSaveReqVO createReqVO) {
        return success(codePartService.createCodePart(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码生成规则组成")
    @PreAuthorize("@ss.hasPermission('auto:code-part:update')")
    public CommonResult<Boolean> updateCodePart(@Valid @RequestBody AutoCodePartSaveReqVO updateReqVO) {
        codePartService.updateCodePart(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码生成规则组成")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('auto:code-part:delete')")
    public CommonResult<Boolean> deleteCodePart(@RequestParam("id") Long id) {
        codePartService.deleteCodePart(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除编码生成规则组成")
                @PreAuthorize("@ss.hasPermission('auto:code-part:delete')")
    public CommonResult<Boolean> deleteCodePartList(@RequestParam("ids") List<Long> ids) {
        codePartService.deleteCodePartListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码生成规则组成")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('auto:code-part:query')")
    public CommonResult<AutoCodePartRespVO> getCodePart(@RequestParam("id") Long id) {
        AutoCodePartDO codePart = codePartService.getCodePart(id);
        return success(BeanUtils.toBean(codePart, AutoCodePartRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码生成规则组成分页")
    @PreAuthorize("@ss.hasPermission('auto:code-part:query')")
    public CommonResult<PageResult<AutoCodePartRespVO>> getCodePartPage(@Valid AutoCodePartPageReqVO pageReqVO) {
        PageResult<AutoCodePartDO> pageResult = codePartService.getCodePartPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AutoCodePartRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码生成规则组成 Excel")
    @PreAuthorize("@ss.hasPermission('auto:code-part:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodePartExcel(@Valid AutoCodePartPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AutoCodePartDO> list = codePartService.getCodePartPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码生成规则组成.xls", "数据", AutoCodePartRespVO.class,
                        BeanUtils.toBean(list, AutoCodePartRespVO.class));
    }

}